home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
4490
/
4490.xpi
/
components
/
scripts
/
imap.js
< prev
next >
Wrap
Text File
|
2010-01-22
|
4KB
|
135 lines
/**********************************************************
IMAP
**********************************************************/
var name="IMAP";
var supportInboxOnly=true;
var supportShowFolders=true;
var needServer=true;
function init(){
this.initStage=ST_DATA;
if(!this.server&&this.user.indexOf("@")!=-1)this.server="imap."+this.user.split("@")[1];
this.seq=0;
}
function processSocket(aData){
if(aData.match(/ OK/)||aData.indexOf("* ")==0){
if(!this.process(null,aData))++this.stage;
}else{
this.onError();
}
}
function process(aHttpChannel, aData) {
switch(this.stage){
case ST_DATA:
var ar=this.server.split(":");
var port=ar[1]?ar[1]:993;
this.sock=new SocketReader(this,ar[0],port,port==993);
return false;
case ST_DATA+1:
var user;
if(this.user.indexOf("|")!=-1)user=this.user.split("|")[0];
else user=this.user.split("@")[0];
this.send("LOGIN "+user+" "+this.password);
return false;
case ST_DATA+2:
this.send("LSUB \"\" \"*\"");
return false;
case ST_DATA+3:
var re=/LSUB \((.*?)\) ".*" "(.+)"/g;
var o;
this.folders=[];
while ((o = re.exec(aData)) != null){
if(!o[2].match(/INBOX/i)&&o[1].indexOf("\Noselect")==-1){
this.folders.push(o[2]);
}
}
this.inboxDone=false;
this.newData=0;
this.folderAr=[];
this.send("SELECT \"INBOX\"");
return false;
case ST_DATA+4:
this.send("SEARCH UNSEEN");
return false;
case ST_DATA+5:
var fnd=aData.match(/\* SEARCH ?(.*)$/m);
if(fnd){
var n=fnd[1]?fnd[1].split(" ").length:0;
if(this.inboxOnly){
if(!this.inboxDone){
this.newData=n;
}
}else{
this.newData+=n;
}
if(this.inboxDone&&n>0){
this.folderAr.push(this.curFolder);
this.folderAr.push(n);
}
if(!this.inboxDone)this.inboxDone=true;
if(this.folders.length>0){
var folder=this.folders.pop();
var name=folder.match(/&(.+)-/);
if(name){
var conv = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"]
.createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
conv.charset="UTF-7";
name=conv.ConvertToUnicode("+"+name[1]);
}else name=folder;
this.curFolder=name;
this.send("SELECT \""+folder+"\"");
this.stage-=1;
return true;
}
var cnt=this.main.wuGetVal(this.id,this.user,0);
cnt=cnt?parseInt(cnt):0;
var num=this.newData;
if(num>=cnt){
this.mailCount=num-cnt;
}else{
this.main.wuSetVal(this.id,this.user,0,num);
this.mailCount=0;
}
this.mailData=this.getData(aData);
if(this.showFolders){
if(this.folderAr)this.mailData.folders=encodeArray(this.folderAr);
}
delete this.folderAr;
this.mailData.desc=this.getDesc();
}
this.send("LOGOUT");
return false;
case ST_DATA+6:
this.sock.close();
if(this.mailCount<0)this.reset();
else this.stage=ST_DATA;
this.main.setState(nsIWebMailNotifier.ST_MAILDATA,this.ind);
return true;
}
this.onError();
return true;
}
function stop(){
if(this.sock)this.sock.close();
}
function send(cmd){
++this.seq;
var s=this.seq.toString();
while(s.length<3)s="0"+s;
this.sock.write("A"+s+" "+cmd+"\r\n");
}
function calcCount(){
return this.mailCount;
}
function getMailURL(){
this.main.wuSetVal(this.id,this.user,0,this.newData);
delete this.newData;
this.mailCount=0;
if(!this.main.newMailsOnly){
this.mailData.desc=this.getDesc();
this.main.setState(nsIWebMailNotifier.ST_MAILDATA,this.ind);
}
return null;
}